namespace Microsoft.AspNetCore.Mvc.Formatters;

public class RawRequestBodyFormatter : InputFormatter
{
    public RawRequestBodyFormatter()
    {
        SupportedMediaTypes.Add("application/octet-stream");
    }

    public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
    {
        var request = context.HttpContext.Request;
        using var memoryStream = new MemoryStream();
        await request.Body.CopyToAsync(memoryStream);
        return await InputFormatterResult.SuccessAsync(memoryStream.ToArray());
    }

    protected override bool CanReadType(Type type)
    {
        return type == typeof(byte[]);
    }
}
